﻿#region File and License Information
/*
<File>
	<Copyright>Copyright © 2009, Daniel Vaughan. All rights reserved.</Copyright>
	<License>
	This file is part of Daniel Vaughan's Core Library

	Daniel Vaughan's Core Library is free software: you can redistribute it and/or modify
	it under the terms of the GNU Lesser General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	Daniel Vaughan's Core Library is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Lesser General Public License for more details.

	You should have received a copy of the GNU Lesser General Public License
	along with Daniel Vaughan's Core Library.  If not, see http://www.gnu.org/licenses/.
	</License>
	<Owner Name="Daniel Vaughan" Email="dbvaughan@gmail.com"/>
	<CreationDate>2010-08-18 17:44:10Z</CreationDate>
</File>
*/
#endregion

using System;
using System.Windows;

namespace DanielVaughan.Services.Implementation
{
	public class MessageService : MessageServiceBase
	{
		public override MessageResult ShowCustomDialog(string message, string caption, 
			MessageButton messageButton, MessageImage messageImage, MessageImportance? importanceThreshold, 
			string details)
		{
			/* If the importance threshold has been specified 
			 * and it's less than the minimum level required (the filter level) 
			 * then we don't show the message. */
			if (importanceThreshold.HasValue && importanceThreshold.Value < MinumumImportance)
			{
				return MessageResult.OK;
			}

			if (!UISynchronizationContext.Instance.InvokeRequired)
			{	/* We are on the UI thread, and hence no need to invoke the call.*/
				var messageBoxResult = MessageBox.Show(message, caption,
					messageButton.TranslateToMessageBoxButton());
				return messageBoxResult.TranslateToMessageBoxResult();
			}

			MessageResult result = MessageResult.OK;
			UISynchronizationContext.Instance.InvokeAndBlockUntilCompletion(
				delegate
				{
					var messageBoxResult = MessageBox.Show(message, caption,
					messageButton.TranslateToMessageBoxButton());
					result = messageBoxResult.TranslateToMessageBoxResult();
				});

			return result;
		}

		static MessageBoxButton ConvertToSilverlightButton(MessageButton messageButton)
		{
			switch (messageButton)
			{
				case MessageButton.OK:
				case MessageButton.YesNo:
					return MessageBoxButton.OK;
				case MessageButton.OKCancel:
				case MessageButton.YesNoCancel:
					return MessageBoxButton.OKCancel;
					default:
					throw new InvalidOperationException(
						"Unknown MessageButton " + messageButton);
			}
		}
	}
}
